$(function(){
	//获取商品数据
	$.ajax({
		'url':'./public/json/brand.json',
		'type':'get',
		'dataType':'json',
		'success':function(str){
			jsonInput(str.goods1);
		}
	})
	function jsonInput(str){
		var current	= str;
		$('<li class="goods-brand">'+ current.brand +'</li>').appendTo($('.goods-deslist'));
		$('<li class="goods-title">'+ current.title +'</li>').appendTo($('.goods-deslist'));
		$('<li class="goods-id border-bot">货号：'+ current.id +'</li>').appendTo($('.goods-deslist'));
		$('<li class="border-bot"><span class="goods-price">￥'+ current.price +'</span><del class="goods-del">专柜价：￥'+current.price+'</del></li>').appendTo($('.goods-deslist'));
		$('<li class="goods-memory"><span class="des-list">机身内存</span></li>').appendTo($('.goods-deslist'));
		// current.memory.forEach(function(index){
			for(var i in current.memory){
				$('<button class="goods-option">'+ current.memory[i] +'</button>').appendTo($('.goods-memory'));	
			}
			
		// })
		$('<li class="goods-color"><span class="des-list">色系</span><button class="goods-option">'+ current.color +'</button></li>').appendTo($('.goods-deslist'));
		$('<li class="num border-bot"><span class="des-list">数量</span><button class="num-button-re">-</button><input class="goods-option" style="margin:0px;width:50px;height:30px;text-align:center;margin:0 5px;" value="1" alt="Num"><button class="num-button-add">+</button></li>').appendTo($('.goods-deslist'));
		$('<li><span class="prompt">发货门店</span><span>此货品将由<a href="#" style="color:#c8a985;text-decoration:none;">第五大道奢侈品网</a>为您发货。</span></li>').appendTo($('.goods-deslist'));
		$('<li><span class="prompt">发货时效</span><span>预计20-25个工作日发货，延迟发货慢必赔！查看详情 》</span></li>').appendTo($('.goods-deslist'));
		$('<li><span class="prompt">温馨提示</span><span>本商品 不支持货到付款 有质量问题支持7天退换货</span></li>').appendTo($('.goods-deslist'));
		$('<li><span class="prompt">包邮政策</span><span>白金钻石顺丰包邮，注册用户满99元免邮（稀奇艺术品牌及货到付款除外）</span></li>').appendTo($('.goods-deslist'));
		$('<li class="goods-btn"><button class="btn-click">立即购买</button><button class="addcart"><i class="iconfont">&#xe603;</i>加入购物袋</button></li>').appendTo($('.goods-deslist'));
		$('<li class="ifont"><span><i class="iconfont">&#xe616;</i>收藏</span><span><i class="iconfont">&#xe615;</i>分享</span><span><i class="iconfont">&#xe618;</i>品牌官方授权</span><span><i class="iconfont">&#xe617;</i>全球速递</span><span><i class="iconfont">&#xe610;</i>商务部诚信企业</span></li>').appendTo($('.goods-deslist'));
		

		//默认选择
		$('.goods-memory .goods-option').eq(0).addClass('active').attr('alt','true');
		$('.goods-color .goods-option').eq(0).addClass('active').attr('alt','true');;
		//选择商品的类型
		$('.goods-memory .goods-option').each(function(){
			$(this).click(function(){
				$(this).css({'border':'1px solid #d00000'}).attr('alt','true').siblings('button').css({'border':'1px solid #c9c9c9'}).attr('alt','');

				
			})
		})

		//改变商品的数量
		$('.num-button-re').click(function() {
			var i = $('[alt="Num"]').val();
			if(i>=1){
				i--;
				$('[alt="Num"]').val(i);
			}
		});

		$('.num-button-add').click(function() {
			var i = $('[alt="Num"]').val();
			if(i<=100){
				i++;
				$('[alt="Num"]').val(i);
			}
		});
		//调用数据
		var n = 0;
		var arrNum = [];
		var totalNum = 0;
		$('.addcart').click(function(event) {
			var id = current.id;
			var brand = current.brand;
			var title = current.title;
			var price = current.price;
			var img = current.img;
			var memory = $('.goods-memory .goods-option[alt="true"]').text();
			var color = $('.goods-color .goods-option[alt="true"]').text();				
			var num = $('.num .goods-option').val();
			var str = id + memory;
			var Num = addToshoppingCar(id,brand,title,price,memory,color,num,img);
			writeInto(str);
			arrNum.push(Num);
			for(var i = 0;i<arrNum.length;i++){
				totalNum += arrNum[i];
			}
			// console.log(totalNum);
			$('.inputcart').show();
		});

		

		//提交数据到cookie
		function addToshoppingCar(id,brand,title,price,memory,color,num,img){
			var _num = 1;
			if(num != undefined)
				_num = Number(num);
			var cookieId = id+memory;
			var totalNum = _num;//总数默认为传入参数
			var cookieSet = {expires:7,path:'/'};//设置cookie路径的
			var jsonStr = '[{"cookidId":"' + id + '","Brand":"'+ brand +'","Title":"'+title+'","Price":"'+ price+'","Memory":"'+memory+'","Color":"'+color+'","Num":"'+ _num +'","Img":"'+ img +'"}]';
			if($.cookie(cookieId)==null){
				$.cookie(cookieId,jsonStr,cookieSet);//没有这个cookie就设置它
			}else{
				var jsonObj = eval('('+$.cookie(cookieId)+')');//把json变为对象
				var findProduct = false; //是否找到产品的ID,找到为True,否则为false
				for(var obj in jsonObj){
					if(jsonObj[obj].cookieId == id){
						jsonObj[obj].Num = Number(jsonObj[obj].Num) + _num;
						totalNum = jsonObj[obj].Num;
						findProduct = true;
						break;
					}
				}
				if(findProduct == false){
					jsonObj[jsonObj.length] = new Object();
					jsonObj[jsonObj.length - 1 ].cookieId = id;
					jsonObj[jsonObj.length - 1 ].Num = Number(num) +1;
				}
				$.cookie(cookieId,JSON.stringify(jsonObj),cookieSet);//写入cookie
			}
			return totalNum;	
		}
			//获取数据之后的购物菜单会用到
		function writeInto(cookieId){
			var str = $.cookie(cookieId);
			var testJson = eval("(" + str + ")");
			if(testJson[1] == undefined){
				num =1;
			}else{
				num = testJson[1].Num;
			}
			price = testJson[0].Price;
			totalmoney = num * price;
			console.log(testJson);
			$('.mid-right span').text(num);
			$('.cartnum').text(num);
			$('.money').text(totalmoney);
		}
	}
	//放大镜
	$(".boxbigimg").jqueryzoom({
		  xzoom: 500,//放大区域宽度
		  yzoom: 500,//放大区域高度
		  preload: 1,//是否显示预加载
		  offset:10,//放大区域偏离小图的距离
		  position: "right",//放大区域显示的位置（left,right）
		  lens:true //是否显示小图上的透明区域
	  });

	var smlpic = $('.boxsmlimg ul li');
	smlpic.each(function(index){
		$(this).click(function(){
			var src = $(this).find('img').attr('src');
			$('.boxbigimg').find('img').attr('src',src);
			$('.boxbigimg').find('img').attr('jqimg',src);
		})
	});
	//关闭购物车显示
	$('#btn-closeshowcart').click(function(){
			$('.inputcart').hide();
		});
	function getCookie(){
		var strCookie = document.cookie;
           var arrCookie = strCookie.split("; ");
            for(var i = 0; i < arrCookie.length; i++){
                var arr = arrCookie[i].split("=");
             	var id = arr[0];
             }
	}
})